package com.parking.bang.rpc.core.server;

import java.util.Map;

import com.parking.bang.rpc.model.ReqMessage;
import com.parking.bang.rpc.model.RespMessage;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Handle receive
 * 
 * @author ivan
 *
 */
public class MessageRecvHandler extends ChannelInboundHandlerAdapter {

	private final Map<String, Object> handlerMap;

	public MessageRecvHandler(Map<String, Object> handlerMap) {
		this.handlerMap = handlerMap;
	}

	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ReqMessage request = (ReqMessage) msg;
		RespMessage response = new RespMessage();
		// init task
		MessageRecvInitializeTask recvTask = new MessageRecvInitializeTask(request, response, handlerMap);
		// handle to server to execute
		RPCServerExecutor.submit(recvTask, ctx, request, response);
	}
	
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }

}
